﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebManager;
using Maticsoft.DBUtility;//请先添加引用

namespace BaoJian.WebManager.Manager.RelateCol
{
    public partial class New : System.Web.UI.Page
    {
        LYQ.BLL.SystemManagerTree treeBLL = new LYQ.BLL.SystemManagerTree();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindRegion();
                BindData(); 
            }
        }
        private void BindTreeData(TreeView treeView)
        {
            TreeNode root = new TreeNode();
            treeView.Nodes.Clear();          

            if (Region.SelectedValue != "beijing")              
            {
                root.Text = "地方版栏目";
                root.Value = "fd870d78-0db5-48d5-af31-4d6acc3fa90d";           
            }
            else
            {
                root.Text = "总站栏目";
                root.Value = "eb8b49b9-e760-497b-83cf-d9fd1d9f0833";      
            }
            treeView.Nodes.Add(root);    
            PopulateTreeView(root);
            treeView.ExpandAll();

        }

        private void PopulateTreeView(TreeNode parentNode)
        {
            string where = " ParentID = '" + parentNode.Value + "'";
            DataSet ds = treeBLL.GetList(where);
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                DataRow row = ds.Tables[0].Rows[i];
                TreeNode node = new TreeNode();
                node.Text = row["OName"] as string;
                node.Value = row["OID"] as string;
                parentNode.ChildNodes.Add(node);
                PopulateTreeView(node);
            }
        }

        private void BindRegion()
        {
            LYQ.BLL.S_Group groupbll = new LYQ.BLL.S_Group();
            Region.DataSource = groupbll.GetList("GName !='系统管理员'").Tables[0];
            Region.DataBind();
        }

        private void BindData()
        {

            PriColumnTreeView.Nodes.Clear();
            AuxiColumn1TreeView.Nodes.Clear();
            AuxiColumn2TreeView.Nodes.Clear();
            AuxiColumn3TreeView.Nodes.Clear();
            AuxiColumn4TreeView.Nodes.Clear();

            BindTreeData(PriColumnTreeView);
            BindTreeData(AuxiColumn1TreeView);
            BindTreeData(AuxiColumn2TreeView);
            BindTreeData(AuxiColumn3TreeView);
            BindTreeData(AuxiColumn4TreeView);           
            
            LYQ.BLL.SysObj sysbll = new LYQ.BLL.SysObj();  
            ListItem item = new ListItem("请选择", "-1");
            string path = Server.MapPath(@"~/uploads/ad/" + Region.SelectedValue);
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(path);
            System.IO.FileInfo[] info = dir.GetFiles();
            DataTable temp = MakeTable(info);

            ImagesList1.DataSource = temp;
            ImagesList1.DataBind();
            ImagesList1.Items.Add(item);

            ImagesList2.DataSource = temp;
            ImagesList2.DataBind();
            ImagesList2.Items.Add(item);

            ImagesList3.DataSource = temp;
            ImagesList3.DataBind();
            ImagesList3.Items.Add(item);


            ImagesList4.DataSource = temp;
            ImagesList4.DataBind();
            ImagesList4.Items.Add(item);

            ImagesList5.DataSource = temp;
            ImagesList5.DataBind();
            ImagesList5.Items.Add(item);
          
        }

        private DataTable MakeTable(System.IO.FileInfo[] info)
        {

            System.Data.DataTable table = new DataTable("Table");

            DataColumn column;
            DataRow row;

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "filename";
            column.ReadOnly = true;
            column.Unique = true;
            table.Columns.Add(column);

            column = new DataColumn();
            column.DataType = System.Type.GetType("System.String");
            column.ColumnName = "filevalue";

            table.Columns.Add(column);
            foreach (System.IO.FileInfo file in info)
            {
                row = table.NewRow();
                row["filename"] = file.Name;
                row["filevalue"] = file.Name;
                table.Rows.Add(row);
            }

            return table;
        }


        protected void Save_Click(object sender, EventArgs e)
        {
            
            for (int i = 0; i < PriColumnTreeView.CheckedNodes.Count; i++)
            {             
                AddRelatedColumn(PriColumnTreeView.CheckedNodes[i].Value);              
                AddRelateImage(PriColumnTreeView.CheckedNodes[i].Value);
            }
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/List.aspx";
            Page.Response.Redirect(url);
        }


        private void AddRelatedColumn(string curSelPriColumnID)
        {
            LYQ.BLL.RelatedColumn bll = new LYQ.BLL.RelatedColumn();
            LYQ.Model.RelatedColumn model = new LYQ.Model.RelatedColumn();

            //首先要检测是否存在该记录，若存在则先删除而后添加
            //TODO: Delete

            if(bll.Exists(SelectPage.SelectedValue,curSelPriColumnID,Region.SelectedValue))
            {
                bll.Delete(SelectPage.SelectedValue, curSelPriColumnID, Region.SelectedValue);
            }

            model.PriColumnID = curSelPriColumnID; //PriColumnID.SelectedValue;
            model.SumAuxi = 0;
            model.SID = Tools.Common.GetMaxSIDAddedONE("RelatedColumn");
            model.Region = Region.SelectedValue;
            model.Page = SelectPage.SelectedValue;
            model.Description = txtDescription.Text;
            model.AuxiColumnID1 = "";
            model.AuxiColumnID2 = "";
            model.AuxiColumnID3 = "";
            model.AuxiColumnID4 = "";
            model.AuxiColumnID5 = "";
            model.AuxiColumnID6 = "";

            if (AuxiColumn1TreeView.SelectedValue != "")
            {
                model.SumAuxi++;
                model.AuxiColumnID1 = AuxiColumn1TreeView.SelectedValue;
            }
            if (AuxiColumn2TreeView.SelectedValue != "")
            {
                model.SumAuxi++;
                model.AuxiColumnID2 = AuxiColumn2TreeView.SelectedValue;
            }

            if (AuxiColumn3TreeView.SelectedValue != "")
            {
                model.SumAuxi++;
                model.AuxiColumnID3 = AuxiColumn3TreeView.SelectedValue;
            }
            if (AuxiColumn4TreeView.SelectedValue != "")
            {
                model.SumAuxi++;
                model.AuxiColumnID4 = AuxiColumn4TreeView.SelectedValue;
            }

            model.Description = txtDescription.Text;
            bll.Add(model);
        }

        private void AddRelateImage(string curSelPriColumnID)
        {
            LYQ.BLL.RelateImage bll = new LYQ.BLL.RelateImage();
            LYQ.Model.RelateImage model = new LYQ.Model.RelateImage();

            if (bll.Exists(SelectPage.SelectedValue, curSelPriColumnID, Region.SelectedValue))
            {
                bll.Delete(SelectPage.SelectedValue, curSelPriColumnID, Region.SelectedValue);
            }

            model.PriColumnID = curSelPriColumnID;//PriColumnID.SelectedValue;
            model.SID = Tools.Common.GetMaxSIDAddedONE("RelateImage");
            model.Region = Region.SelectedValue;
            model.Page = SelectPage.SelectedValue;
            model.Description = txtDescription.Text;
            model.SumImage = 0;
            model.ImagePath1 = "";
            model.URL1 = "";
            model.ImagePath2 = "";
            model.URL3 = "";
            model.ImagePath4 = "";
            model.URL3 = "";
            model.ImagePath4 = "";
            model.URL4 = "";
            model.ImagePath5 = "";
            model.URL5 = "";
            model.ImagePath6 = "";
            model.URL6 = "";

            if (Image1.Text != "")
            {
                model.ImagePath1 = Image1.Text;
                model.URL1 = URL1.Text;
                model.SumImage++;
            }

            if (Image2.Text != "")
            {
                model.ImagePath2 = Image2.Text;
                model.URL2 = URL2.Text;
                model.SumImage++;
            }

            if (Image3.Text != "")
            {
                model.ImagePath3 = Image3.Text;
                model.URL3 = URL3.Text;
                model.SumImage++;
            }


            if (Image4.Text != "")
            {
                model.ImagePath4 = Image4.Text;
                model.URL4 = URL4.Text;
                model.SumImage++;
            }

            if (Image5.Text != "")
            {
                model.ImagePath5 = Image5.Text;
                model.URL5 = URL5.Text;
                model.SumImage++;
            }
            bll.Add(model);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            
            string url = URLUtility.UrlBase + @"/WebManager/manager/RelateCol/List.aspx";
            Page.Response.Redirect(url);
        }

        protected void Region_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void ImagesList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image1.Text = ImagesList1.SelectedValue;
            SetImagePath(ImagesList1.SelectedValue);
        }
        protected void ImagesList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image2.Text = ImagesList2.SelectedValue;
            SetImagePath(ImagesList2.SelectedValue);
        }
        protected void ImagesList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image3.Text = ImagesList3.SelectedValue;
            SetImagePath(ImagesList3.SelectedValue);
        }
        protected void ImagesList4_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image4.Text = ImagesList4.SelectedValue;
            SetImagePath(ImagesList4.SelectedValue);
        }
        protected void ImagesList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            Image5.Text = ImagesList5.SelectedValue;
            SetImagePath(ImagesList5.SelectedValue);
        }
        private void SetImagePath(string imgName)
        {
            if (!imgName.ToLower().Contains(".swf"))
            {
                string path = @"~/uploads/ad/" + Region.SelectedValue;
                path += @"/" + imgName;
                ShowImage.ImageUrl = path;
            }
        }


    }
}
